<?php
namespace app\common;
use think\Model;
use think\Db;

class StudentModel extends Model{
    
	public function getStudentTotal($page, $rows, $where){
        $result = Db::view('班主任科任','ID,年级号 grade,班号 clazz, 年级号, 班号, 学籍类型, 班主任ID')
                ->view('教师信息','姓名 as banzhuren','班主任科任.班主任ID=教师信息.ID','LEFT')
                ->where($where)
                ->page($page, $rows)
                ->order(array('grade'=>'desc','clazz'=>'asc'))
                ->select();
            $count = Db::table("班主任科任")->where($where)->count();
            $grid['rows'] = $this::_get_result(selectResult($result));
            $grid['total'] = $count;
            return $grid;
	}

    public function getStudentList($page, $rows, $fields, $where, $orderby){
        $result = Db::table("学生信息")
                ->field($fields)
                ->where($where)
                ->order($orderby ? $orderby : "学籍类型,年级号 desc,班号,学号")
                ->page($page, $rows)
                ->select();
        $count = Db::table("学生信息")->where($where)->count();
        $grid['rows'] = $this::_get_result(selectResult($result));
        $grid['total'] = $count;
        return $grid;
    }

    static protected function _get_result($data) {
        $list = [];
        foreach($data as $key => $obj){
            $result = $obj;
            foreach($obj as $key2 => $value){
                if($key2 == 'grade' || $key2 == '年级号') {
                    $result['gradeName'] = getCommonGradeName($value,$obj["学籍类型"]);
                }
            }
            $list[$key] = $result;
        }
        return $list;
    }
}
